﻿using System;
using System.Collections.Generic;
using Generator.Selections.DataBase;
using Generator.Selections.DataDictionary;

namespace Generator.Selections.Repository
{
    class RelationRepository
    {
        internal static void GetRelations(List<Table> tableList)
        {
            try
            {
                ConnectionHandler.Instance.Connection.Open();
                GetRelation(tableList);
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.ToString());
            }
            finally
            {
                ConnectionHandler.Instance.Connection.Close();
            }
        }

        private static void GetRelation(List<Table> tableList)
        {
            foreach (var table in tableList)
            {
                using (var cmd = DataDictionaryCommands.GetRelationsCommand(table.TableName))
                {
                    using (var rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            var tableName = (rdr[0].ToString());
                            var relation = new Relation(tableName);
                            relation.Generate = true;
                            table.RelationList.Add(relation);
                        }
                    }
                }
            }
        }
    }
}
